#!/user/bin/env jython
# -*- coding: utf-8 -*-

from javax.swing import JFrame, JButton, JPanel, JMenuBar, JMenu, JMenuItem, BoxLayout
from java.awt import *
from java.lang import Runnable
from JCostumeQueries import *
from JDefaultQueries import *
from JStartTask import *
from PServer import *
import os
import sys

class Statistics:
	
	def __init__(self):
		
		print "entrei nisto"
		
		
		self.frame = JFrame('Hello, Jython!',
            defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
            size = (800, 600))
        
            
		
		
		#self.frame.setResizable(False)
		self.showFrame()
		self.makeFrame()
		self.make_menuBar()
		self.make_subMenus()
		self.make_buttons()
		self.statusBar()
		self.showFrame()
		
		if os.path.exists("Rebides.db") == True:
			print "ok"
			pass
		else:
			JStartTask(self.main_Panel)
			self.showFrame()
			pass
		
			
		pass
	pass

	def makeFrame(self):
		
		self.main_Panel = JPanel()
		#self.main_Panel.setBackground(Color.RED)
		#self.main_Panel.setMinimumSize(Dimension(800,500))
		#self.main_Panel.setPreferredSize(Dimension(800,525))
		self.main_Panel.setMaximumSize(Dimension(800,525))
		self.main_Panel.layout = BoxLayout(self.main_Panel, BoxLayout.Y_AXIS)

		self.frame.add(self.main_Panel, BorderLayout.CENTER)

	def showFrame(self):
		self.frame.setVisible(True)
		self.frame.repaint()
 
	def make_menuBar(self):

		self.menuBar = JMenuBar()
		
		self.fileMenu = JMenu("File")
		self.queryMenu = JMenu("Query")
		self.serverMenu = JMenu("Server")
############################################################
		self.menuBar.add(self.fileMenu)
		self.menuBar.add(self.queryMenu)
		self.menuBar.add(self.serverMenu)
############################################################
		self.frame.setJMenuBar(self.menuBar)
		self.showFrame()
		print "entrei aqui"

	def make_subMenus(self):

##Sub menus do menu Query
		
		self.exitOption = JMenuItem("Exit", actionPerformed = self.exitEvent)
		self.fileMenu.add(self.exitOption)
		self.defaultQuerys = JMenuItem("Default Queries", actionPerformed = self.defaultQuerieEvent)
#############################################################
		self.queryMenu.add(self.defaultQuerys)
		


#############################################################
		self.startServer = JMenuItem("Start http server", actionPerformed = self.startServerEvent)
		self.stopServer = JMenuItem("Stop http server", actionPerformed = self.stopServerEvent)

#############################################################
		self.serverMenu.add(self.startServer)
		self.serverMenu.add(self.stopServer)
#############################################################

		self.showFrame()

	def make_buttons(self):
		print "teste"
		
	def costumeQuerieEvent(self, event):
		JCostumeQueries(self.main_Panel, self.statusBarInfo)
		self.showFrame()
		
	def defaultQuerieEvent(self, event):
		self.main_Panel.removeAll()
		self.frame.repaint()
		JDefaultQueries(self.main_Panel, self.frame, self.statusBarInfo)
	
		self.showFrame()
		
	def statusBar(self):
		statusPanel = JPanel()
		statusPanel.setMaximumSize(Dimension(800, 10))
		#statusPanel.setMinimumSize(Dimension(800, 10))
		#statusPanel.setPreferredSize(Dimension(800, 10))
		statusPanel.layout = FlowLayout(FlowLayout.LEFT)
		statusPanel.setBackground(Color.BLACK)
		
		self.statusBarInfo = JLabel()
		self.statusBarInfo.setText("Ready!")
		self.statusBarInfo.setFont(Font("Arial", Font.BOLD, 10))
		self.statusBarInfo.setForeground(Color.WHITE)
		
		statusPanel.add(self.statusBarInfo)
		
		self.frame.add(statusPanel, BorderLayout.SOUTH)	
	
	def startServerEvent(self, event):
		self.ps = PServer()
		self.ps.start()
		self.statusBarInfo.setText("Server http running")
		self.showFrame()
		
		
		pass
		
	def stopServerEvent(self, event):
		
		self.ps.stopServer()
		self.statusBarInfo.setText("Ready!")
		self.showFrame()
		
		pass
	
	def exitEvent(self, event):
		
		sys.exit(0)


if __name__ == '__main__':
	Statistics()



